/*
 * Based on Obdev's AVRUSB code and under the same license.
 *
 * TODO: Make a proper file header. :-)
 */
#ifndef __UsbPacket_h__
#define __UsbPacket_h__

#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <string.h>

#ifdef __cplusplus
extern "C"{
#endif
#include "usbdrv.h"
void USBSetProductName(char *string);
#ifdef __cplusplus
} // extern "C"
#endif

// TODO: Work around Arduino 12 issues better.
//#include <WConstants.h>
//#undef int()
#if !defined(TIMSK0) && defined(TIMSK)
#define TIMSK0 TIMSK
#endif

typedef uint8_t byte;

#include <util/delay.h>     /* for _delay_ms() */

// Greetings from the CAN bus :-)
#ifndef PACKET_SIZE
#define PACKET_SIZE 8
#endif

#ifndef RING_BUFFER_SIZE
#define RING_BUFFER_SIZE 8
#endif

struct usbpacket {
  uint8_t byte[PACKET_SIZE];   
};

struct ring_buffer {
  struct usbpacket buffer[RING_BUFFER_SIZE];
  int head;
  int tail;
};





class UsbPacketDevice {
 private:
  ring_buffer *_rx_buffer;
  ring_buffer *_tx_buffer;

 public:
  UsbPacketDevice (ring_buffer *rx_buffer, ring_buffer *tx_buffer);

  void begin(char *ProductNameString);
    
  // TODO: Deprecate update
  void update();

  void refresh();

  int available();
  int read(struct usbpacket *p);
  int write(struct usbpacket *p);

  uchar    reportBuffer[PACKET_SIZE];
};

extern UsbPacketDevice UsbPacket;

#endif // __UsbPacket_h__
